/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.teacake.filteringproxy;
import java.io.*;
/**
 *
 * @author tkleisas
 */
public class Utility {
    public static final int find(byte[] buffer, byte[] searchbuffer)
    {
        int retval = -1;

        for(int i=0;i<buffer.length-searchbuffer.length+1;i++)
        {
            boolean found = true;
            for(int l=0;l<searchbuffer.length;l++)
            {
                if(buffer[i+l]!=searchbuffer[l])
                {
                    found = false;
                }
            }
            if(found)
            {
                return i;
            }
        }
        return retval;
    }
    public static final int find(byte[] buffer, byte[] searchbuffer,int startpos)
    {
        int retval = -1;

        for(int i=startpos;i<buffer.length-searchbuffer.length+1;i++)
        {
            boolean found = true;
            for(int l=0;l<searchbuffer.length;l++)
            {
                if(buffer[i+l]!=searchbuffer[l])
                {
                    found = false;
                }
            }
            if(found)
            {
                return i;
            }
        }
        return retval;
    }
    public static byte[] append(byte[] buffer1, byte[] buffer2)
    {
        byte[] buffer = null;
        if(buffer1 == null)
        {
            return buffer2;
        }
        if(buffer2 == null)
        {
            return buffer1;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(buffer1, 0, buffer1.length);
        baos.write(buffer2, 0, buffer2.length);
        buffer = baos.toByteArray();
        return buffer;
    }
    public static byte[] replace(byte[] str, byte[] oldstr, byte[] newstr)
    {
        int index = find(str,oldstr);
        int startpos=0;
        byte[] head = null;
        byte[] tail = null;
        byte[] result = null;
        while(index>=0)
        {
            head = Utility.copyFromBuffer(str, startpos, index-startpos);
            result = append(result,head);
            result = append(result,newstr);
            startpos = index+oldstr.length;
            index = find(str,oldstr,startpos);
            

            
        }
        tail = copyFromBuffer(str,startpos,str.length-startpos);
        result = append(result,tail);
        
        return result;
    }
    public static byte[] copyFromBuffer(byte[] buffer, int start, int length)
    {
        ByteArrayOutputStream tempbuffer= new ByteArrayOutputStream();
        tempbuffer.write(buffer, start, length);
        return tempbuffer.toByteArray();
    }
    public static void main(String[] args)
    {
        byte[] str1 = null;
        byte[] str2 = null;
        byte[] str3 = "0123456789hello89jjj".getBytes();
        byte[] str4 = "z".getBytes();
        byte[] str5 = "x".getBytes();
        str1 = Utility.append(str1, str2);
        str1 = Utility.append(str1, str3);
        str1 = Utility.append(str1, str4);
        str2 = copyFromBuffer(str1,1,2);
        str3 = replace(str3,str4,str5);
        System.out.println(new String(str3));
        int index = find(str1,str4);

    }
}
